package org.example.service;

import lombok.extern.slf4j.Slf4j;
import org.example.dto.AccessTokenDTO;
import org.example.dto.WxOfficalTicket;
import org.example.dto.WxOfficalUserInfo;
import org.example.utils.WxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WxOfficalApiClient {
    @Autowired
    private WxUtil wxUtil;


    public String getQrCode(String appId, String appSecret, String scene) {
        AccessTokenDTO accessToken = wxUtil.getAccessToken();
        log.info("accessToken: {}", accessToken);
        if (accessToken == null) {
            return null;
        }
        if (accessToken.getErrCode() != null) {
            log.error("获取access_token错误 {}", accessToken.getErrMsg());
            return null;
        }
        WxOfficalTicket ticket = wxUtil.getTicket(accessToken.getAccess_token(), scene);
        if (ticket == null) {
            return null;
        }
        if (ticket.getErrCode() != null) {
            log.error("获取ticket错误 {}", ticket.getErrMsg());
            return null;
        }
        return wxUtil.getQrCode(ticket.getTicket());
    }

    public WxOfficalUserInfo getUserInfo(String openId) {
        AccessTokenDTO accessToken = wxUtil.getAccessToken();
        WxOfficalUserInfo userInfo = wxUtil.getUserInfo(accessToken.getAccess_token(), openId);
        if (userInfo != null) {
            if (userInfo.getErrCode() != null) {
                log.error("获取微信info错误 {}", userInfo.getErrMsg());
            } else {
                return userInfo;
            }
        }
        return null;
    }

}
